package com.StarStudio.Utils;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

public class test0701 {
    public static void main(String[] args) throws Exception {
       /* Map<Integer, Integer> map = new HashMap<>();
        map.put(3,3);
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getKey() == 3){
                System.out.println("true" + entry.getValue());
            }
        }
        Timestamp timestamp = new Timestamp(new Date().getTime());
        System.out.println(timestamp);
        System.out.println(new Date().getTime());*/

        /*
        params.put("appId","ZYSC_YB");
        params.put("secert","123456");
        params.put("token","684971499c4f7eb7b0c891c94e567b1ef1d6cac92e72ffc9d1ed84b1bf3995da");
        params.put("idCard","51253419720212703X");
        String resp = HttpPostUtils.HttpPost(URL, params);
        JSONObject respObject = JSONObject.parseObject(resp);
        Object data = respObject.get("data");
        UserInfoPostBean userInfoPostBean = JSON.parseObject(data.toString(), UserInfoPostBean.class);
        System.out.println(userInfoPostBean);
        System.out.println(userInfoPostBean.getCardType());*/
        /*
        String token = HttpRequestUtils.getProvinceSystemToken();
        System.out.println(token);
        String resp = HttpRequestUtils.ProvinceSystemBeanAction(3051489, -5);
        System.out.println(resp);

        System.out.println(new Date().getTime());*/

//        String decrypt = DESedeUtils.decrypt("d8ad1b0201023bccb933216e53dae1aa", "6ccdb02c7d22038018ad5a69938560945998d369ab4ed838a540c79cba0ae24e");
//        System.out.println(decrypt);

//        System.out.println(new Timestamp(new Date().getTime()).toString().split(" ")[0]);
//        int[] nums = new int[]{1,1,1,1,5,6};
//        int i = 0,j=5;
//        while(nums[i] == nums[++i]);
////        while (nums[i] == nums[i +1]){
////            i++;
////        }
//        System.out.println(i);

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
            int T = Integer.parseInt(reader.readLine());
            for (int i = 0; i < T; i++) {
                int N = Integer.parseInt(reader.readLine());
                String tables = reader.readLine();
                int M = Integer.parseInt(reader.readLine());
                String enters = reader.readLine();

                int[] res = solve(tables, enters);
                for (int r : res) {
                    writer.write(Integer.toString(r));
                    writer.newLine();
                }
            }
            writer.flush();
        }

        private static int[] solve(String tables, String enters) {
            List<PriorityQueue<Integer>> pqs = new ArrayList<>(3);
            pqs.add(new PriorityQueue<>());
            pqs.add(new PriorityQueue<>());
            pqs.add(new PriorityQueue<>());
            for (int i = 0; i < tables.length(); i++) {
                pqs.get(tables.charAt(i) - '0').add(i);
            }
            int[] res = new int[enters.length()];
            for (int i = 0; i < enters.length(); i++) {
                int table;
                if (enters.charAt(i) == 'M') {
                    if (pqs.get(1).isEmpty()) {
                        table = pqs.get(0).poll();
                        pqs.get(1).add(table);
                    } else {
                        table = pqs.get(1).poll();
                        pqs.get(2).add(table);
                    }
                } else {
                    if (!pqs.get(0).isEmpty()) {
                        table = pqs.get(0).poll();
                        pqs.get(1).add(table);
                    } else {
                        table = pqs.get(1).poll();
                        pqs.get(2).add(table);
                    }
                }
                res[i] = table + 1;
            }

            return res;

    }
}
